package com.lagou.mrsort;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;

public class SortReducer extends Reducer<IntWritable, NullWritable, IntWritable, IntWritable> {

    /*
        reduce阶段的输入k,v即为map阶段输出的k,v
        输出key为排名，value为数字
     */
    IntWritable id = new IntWritable();
    int i = 1;

    @Override
    protected void reduce(IntWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException, IOException {

        // 遍历values，输出数据
        for (NullWritable value : values) {
            id.set(i++);
            context.write(id, key);
        }
    }
}
